<?php namespace Admin\Controller;
class GoodsController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\Goods;
	}
	public function index(){

		$data=$this->model
			 ->join("detail","goods.gid","=","detail.gid")
			 ->get();
		
		View::with('data',$data);
		View::make();
	}
	public function add(){
		
		if(IS_POST){
			
			if($this->model->store()){
				View::success("添加成功",U('Goods/index'));
			}
			View::error($this->model->getError());
		}
		$cateModel=new \Common\Model\Cate;
		$cate=$cateModel->get();
		$cate=Data::tree($cate,'cname','cid','pid');
		// p($cate);
		$brandModel=new \Common\Model\Brand;
		$brand=$brandModel->get();
		View::with('cate',$cate);
		View::with('brand',$brand);
		View::make();
	}
	  // 上传文件
    public function upload()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}
	// public function delpic(){
	// 	p(Q("post.a"));
	// 	exit;
	// 	unlink(Q("post.a"));
	// }

	// 获得所属类型属性的数据
	public function getAttr(){
		//得到类型id(NoteWork-------XHR----preview打印)
		$tid = Q('post.t',0,'intval');
		$typeAttrModel=new \Common\Model\TypeAttr;
		$data=$typeAttrModel->where("type_tid={$tid}")->get();
		
		foreach ($data as $k => $v) {
			// 将字符串循环变成数组，用于循环遍历
			$data[$k]['tavalue']=explode(',', $v['tavalue']);
		}
		//p($data);
		// 转化成json格式
		echo json_encode($data);exit;
		
	}

	//获得编辑当前的数据
	public function getStr(){
		$gid=Q('post.gid',0,'intval');
		$cid=$this->model->where("gid=$gid")->pluck('category_cid');
		$cateModel=new \Common\Model\Cate;
		$tid=$cateModel->where("cid=$cid")->pluck('type_tid');
		$typeModel=new \Common\Model\TypeAttr;
		$newData=$typeModel->where("type_tid=$tid")->get();
		foreach ($newData as $k => $v) {
			// 将字符串循环变成数组，用于循环遍历
			$newData[$k]['tavalue']=explode(',', $v['tavalue']);
		}
		$data="";
		$goodsAttrModel=new \Common\Model\GoodsAttr;
		$oldData=$goodsAttrModel
				->join('type_attr','type_attr_taid','=','taid')
		        ->where("goods_gid=$gid")
		        ->get();

		$data=array(
					'0'=>$oldData,
					'1'=>$newData
		);
		// 转化成json格式
		echo json_encode($data);exit;
	}
	//商品编辑
	public function edit(){


		if(IS_POST){
			if($this->model->edit()){

				View::success("修改成功",U('Goods/index'));
			}
			View::success($this->model->getError());
		}
		// 获取旧数据
		$gid=Q('get.gid',0,'intval');
		$good=$this->model
		     ->join("detail","goods.gid","=","detail.gid")
		     ->where("goods.gid={$gid}")->find();
		
		$good['thumb']=explode(',', $good['thumb']);
		

		// 所属分类
		$cateModel=new \Common\Model\Cate;
		$cateData=$cateModel->get();
		$cateData=Data::tree($cateData,'cname');
		// p($cateData);

		// 所属品牌
		$brandModel=new \Common\Model\Brand;
		$brandData=$brandModel->get();

		//商品属性
		$goodModel=new \Common\Model\GoodsAttr;
		$goodData=$goodModel->join("type_attr","type_attr_taid","=","taid")
				 ->where("goods_gid={$gid}")->get();
		  $taid = [];
		foreach($goodData as $k=>$v){
			$goodData[$k]['tavalue']=explode(',',$v['tavalue']);
			//抓取已经选过的属性id
			$taid[]=$v['type_attr_taid'];
		}
		// 排除已经选过的属性，抓取没有设置的属性
		$taid=implode(',', $taid);
		// p($taid);
		$typeModel=new \Common\Model\TypeAttr;
		$typeData=$typeModel->where("taid NOT IN ({$taid}) AND type_tid={$good['type_tid']}")->get();
		// p($typeData);
		foreach($typeData as $k=>$v){
			$goodData[]=array(
				'taname'=>$v['taname'],
				'tavalue'=>explode(',', $v['tavalue']),
				'gtvalue'=>'',
				'class'=>$v['class'],
				'taid'=>$v['taid']
			);
		}
		// p($goodData);
		View::with('good',$good);
		View::with('cateData',$cateData);
		View::with('brandData',$brandData);
		View::with('goodData',$goodData);
		View::make();
	}

	//删除商品
	public function del(){
		$gid=Q("get.gid",0,'intval');
		$goodModel=new \Common\Model\GoodsAttr;
		$detail=new \Common\Model\Detail;
		$goodModel->where("goods_gid={$gid}")->delete();
		$detail->where("gid={$gid}")->delete();
        $this->model->where("gid={$gid}")->delete();
        $goodList=new \Common\Model\GoodsList;
		$goodList->where("goods_gid=$gid")->delete();
		View::success("删除成功");
	}
}